/*
 * Copyright 2010 GP Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * MappingUtil.java | Created on April 25, 2010
 */
package org.gp.gpetl.util;

import org.gp.gpetl.config.BeanMapping;
import org.gp.gpetl.config.Mapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A utility that maps column name to bean
 *
 * @author Paul Augustine C. Mendoza
 * @since 1.0
 */
public class MappingUtil {

    /**
     * The restricted constructor
     */
    private MappingUtil() {
        super();
    }

    /**
     * Converts the bean mapping to a map
     *
     * @param beanMapping the bean mappings
     * @return the mappings
     */
    public static Map<String, String> toMap(BeanMapping beanMapping) {
        if (!hasValidMapping(beanMapping)) {
            throw new IllegalArgumentException(
                    "Bean mapping cannot be null or empty.");
        }
        Map<String, String> mappings = new HashMap<String, String>();
        for (Mapping mapping : beanMapping.getMappings()) {
            mappings.put(mapping.getColumn(), mapping.getType());
        }
        return mappings;
    }

    /**
     * Converts the bean mapping to headers
     *
     * @param beanMapping the bean mapping
     * @return the headers
     */
    public static String[] toHeaders(BeanMapping beanMapping) {
        if (!hasValidMapping(beanMapping)) {
            throw new IllegalArgumentException(
                    "Bean mapping cannot be null or empty.");
        }
        String[] headers = new String[beanMapping.getMappings().size()];
        int idx = 0;
        for (Mapping mapping : beanMapping.getMappings()) {
            headers[idx++] = mapping.getColumn();
        }
        return headers;
    }

    /**
     * Extracts the data rows only
     *
     * @param records the rows + headers
     * @return the data rows
     */
    public static List<String[]> extractData(List<String[]> records) {
        if (!hasValidRecords(records)) {
            throw new IllegalArgumentException(
                    "Records cannot be null or empty.");
        }
        List<String[]> newList = new ArrayList<String[]>();
        for (int i = 1; i < records.size(); i++) {
            newList.add(records.get(i));
        }
        return newList;
    }

    /**
     * Checks for valid records
     * @param records the records
     * @return true if valid
     */
    private static boolean hasValidRecords(List<String[]> records) {
        if (records == null) {
            return false;
        }
        if (records.size() < 1) {
            return false;
        }
        return true;
    }

    /**
     * Checks for valid bean mapping
     *
     * @param beanMapping the bean mapping
     * @return true if valid
     */
    private static boolean hasValidMapping(BeanMapping beanMapping) {
        if (beanMapping == null) {
            return false;
        }
        if (beanMapping.getMappings() == null) {
            return false;
        }
        if (beanMapping.getMappings().isEmpty()) {
            return false;
        }
        return true;
    }
}
